package web.zz.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import web.zz.bean.User;
import web.zz.service.UserService;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

//    @RequestMapping(value = "/index.html")
//    public String loginPage(){
//        return "login";
//    }
//
////    @RequestMapping(value = "/loginCheck.html")
//    public ModelAndView loginCheck(HttpServletRequest request,LoginCommand loginCommand){
//        boolean isValidUser = userService.hasMatchUser(loginCommand.getUsername(),loginCommand.getPassword());
//        if (!isValidUser){
//            return new ModelAndView("login","error","用户或者密码错误");
//        }else {
//            User user = userService.findUser(loginCommand.getUsername());
//            request.getSession().setAttribute("user",user);
//            return new ModelAndView("main");
//        }
//    }
    @RequestMapping(value = {"index"})
    public String index(HttpServletRequest request,String username,String password){
        return "index";
    }

    @RequestMapping(value = {"login"})
    public ModelAndView login(HttpServletRequest request,String username,String password){
        ModelAndView modelAndView = new ModelAndView();
        User user = userService.findUser(username);
        if (null!=user){
            if (user.getPassword().equals(password)){
                modelAndView.addObject("user",user);
                modelAndView.setViewName("main");
            }else {
                modelAndView.setViewName("index");
            }
        }else {
            modelAndView.setViewName("index");
        }
        return modelAndView;
    }
}
